package com.orangelife.mobile.annoucement.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.curry.android.http.OrangeHttpBiz;
import com.curry.android.http.VolleyHttpUtil;
import com.curry.orangelife.mobile.R;
import com.markupartist.android.widget.PullToRefreshListView;
import com.orangelife.mobile.annoucement.adapter.AnnoucementListAdapter;
import com.orangelife.mobile.annoucement.biz.MessageHttpBiz;
import com.orangelife.mobile.common.activity.PullRefreshBaseActivity2;
import com.orangelife.mobile.common.activity.PullRefreshBaseActivity3;
import com.orangelife.mobile.common.view.EmptyLayout;
import com.orangelife.mobile.util.BLog;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 小区公告列表；系统消息列表。公用一个界面
 * <p>
 * Created by YangWei
 * on 2015/11/10.
 *
 * @author yw
 */
public class AnnoucementListActivity extends PullRefreshBaseActivity3 {

    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.iv_title_right)
    ImageView ivTitleRight;
    @Bind(R.id.pullListView)
    PullToRefreshListView pullListView;
    @Bind(R.id.emptyLayout)
    EmptyLayout emptyLayout;
    private List<Map<String, Object>> list = new ArrayList<>();
    private AnnoucementListAdapter adapter;
    private String type;
    /** 系统消息 */
    public static final String TYPE_MESSAGE = "1";
    /** 小区公告 */
    public static final String TYPE_ANNOUNCEMENT = "2";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_annoucement_list);
        ButterKnife.bind(this);
        getIntentInfo();
        initView();
        initAdapter();
        setXListViewAndEmptyLayoutListener(pullListView, emptyLayout, adapter);
        requestList();
    }

    @Override
    protected boolean isDelayRequest() {
        return true;
    }

    private void getIntentInfo() {
        Intent intent = getIntent();
        type = intent.getStringExtra("type");
    }

    private void initView() {
        if (TYPE_ANNOUNCEMENT.equals(type)) {
            tvTitle.setText("公告列表");
        } else {
            tvTitle.setText("消息中心");
        }
        ivTitleRight.setVisibility(View.GONE);
    }

    /**
     * 给listview设置adapter
     */
    private void initAdapter() {
        adapter = new AnnoucementListAdapter(this, list, type);
    }

    private void requestList() {
        MessageHttpBiz.getList(type, pageNo, pageSize, new VolleyHttpUtil.ResponseCallBack() {
            @Override
            public void handleResponse(JSONObject response, int errCode) {
                dismissProgress();
                List<Map<String, Object>> list = MessageHttpBiz.handleList(response);
                handleAfterRequest(isConnected(), OrangeHttpBiz.getTotalNum(response), list, pullListView, adapter);
            }
        });
    }


    @Override
    protected EmptyLayout getEmptyLayout() {
        return emptyLayout;
    }

    @Override
    protected List getData() {
        return list;
    }

    @Override
    protected int getDataCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    protected void requestWhenPullRefresh() {
        requestList();
    }

    @Override
    protected void onResume() {
        super.onResume();
        BLog.getInstance().intAppmod(BLog.MESSAGE);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        BLog.getInstance().outAppmod(BLog.MESSAGE);
    }
}
